const { Sequelize } = require('sequelize')
const sequelize = require('../config/db')
const moment = require('moment')

// User 模型
const User = sequelize.define('user', {
  id: {
    type: Sequelize.STRING,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true
  },
  username: {
    type: Sequelize.STRING,
    allowNull: false,
    defaultValue: 'milk friends',
    comment: '昵称'
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '登录密码'
  },
  avatar: {
    type: Sequelize.STRING,
    allowNull: false,
    defaultValue: 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2712859483,1666841396&fm=26&gp=0.jpg',
    comment: '用户头像'
  },
  signid: {
    type: Sequelize.INTEGER,
    allowNull: true,
    comment: '用户签名id'
  },
  gender: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户性别'
  },
  birth: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户出生日期'
  },
  intro: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户自我介绍'
  },
  province: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户所在省份'
  },
  city: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户所在城市'
  },
  phone: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户电话'
  },
  email: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户邮箱'
  },
  statusid: {
    type: Sequelize.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '用户状态id(默认是离线状态)'
  },
  createtime: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '用户注册时间',
    defaultValue: moment().format('YYYY-MM-DD HH:mm:ss')
  },
  logintime: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户最后登录时间'
  },
  lasttime: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '用户上一次登录时间'
  },
  enable: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: '用户是否启用',
    defaultValue: 1
  }
}, {
  freezeTableName: true
})

// Role模型
const Role = sequelize.define('role', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '角色名称'
  },
  description: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '角色描述'
  },
  menu: {
    type: Sequelize.TEXT,
    allowNull: true,
    comment: '角色所属菜单',
    defaultValue: '[]'
  },
  permission: {
    type: Sequelize.TEXT,
    allowNull: true,
    comment: '角色所属权限',
    defaultValue: '[]'
  },
}, {
  freezeTableName: true
})

// Permission 模型
const Permission = sequelize.define('permission', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '接口名称'
  },
  path: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '接口地址'
  },
  type: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '接口类型'
  },
  basename: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '上级节点名称'
  },
  basepath: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '上级节点地址'
  },
  show: {
    type: Sequelize.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '是否显示'
  },
  enable: {
    type: Sequelize.INTEGER,
    allowNull: false,
    defaultValue: 1,
    comment: '是否启用'
  }
}, {
  freezeTableName: true
})

// Code 模型
const Code = sequelize.define('code', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    comment: '验证码id',
    allowNull: false
  },
  email: {
    type: Sequelize.STRING,
    comment: '邮箱',
    allowNull: false
  },
  code: {
    type: Sequelize.STRING,
    comment: '验证码',
    allowNull: false
  },
  createtime: {
    type: Sequelize.STRING,
    comment: '验证码生成时间',
    allowNull: false
  }
}, {
  freezeTableName: true
})

// UserStatus 模型
const UserStatus = sequelize.define('user_status', {
  id: {
    type: Sequelize.INTEGER,
    autoIncrement: true,
    allowNull: false,
    comment: '状态id',
    primaryKey: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '状态名称'
  },
  icon: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '状态图标'
  },
  desc: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '状态描述'
  }
}, {
  freezeTableName: true
})

// Frineds 模型
const Friends = sequelize.define('friends', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
    comment: '好友记录id'
  },
  fid: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '好友id'
  },
  uid: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '自己id'
  },
  nickname: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '好友备注'
  },
  fgid: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: '好友分组id'
  },
  type: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '好友添加方式'
  },
  remarks: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: '添加好友时的验证消息'
  },
  createtime: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '好友添加时间',
    defaultValue: moment().format('YYYY-MM-DD HH:mm:ss')
  },
  status: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: '是否已经通过验证',
    defaultValue: 0
  }
}, {
  freezeTableName: true
})

// FriendsGroup 模型
const FriendsGroup = sequelize.define('friends_group', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '分组id'
  },
  uid: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '用户id'
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '分组名称'
  },
  createtime: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: '分组创建时间',
    defaultValue: moment().format('YYYY-MM-DD HH:mm:ss')
  },
  delable: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: '该分组是否可以被删除',
    defaultValue: 0
  }
}, {
  freezeTableName: true
})

// 个性签名
const Sign = sequelize.define('user_sign', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '个签id',
  },
  uid: {
    type: Sequelize.STRING,
    comment: '用户id',
    allowNull: false
  },
  message: {
    type: Sequelize.STRING,
    comment: '个签内容',
    allowNull: false
  },
  location: {
    type: Sequelize.STRING,
    comment: '个签发送位置',
    allowNull: true
  },
  createtime: {
    type: Sequelize.STRING,
    comment: '个签发布时间',
    allowNull: false,
    defaultValue: moment().format('YYYY-MM-DD HH:mm:ss')
  }
}, {
  freezeTableName: true
})

/**
 * 用户User 和角色Role 对应关系
 * 一个用户 -> 一个角色
 * 一个角色 -> 多个用户
 */
Role.hasMany(User, {
  foreignKey: 'rid',
  sourceKey: 'id'
})
User.belongsTo(Role, {
  foreignKey: 'rid',
  targetKey: 'id'
})

/**
 * 用户User 和状态 UserStatus 对应关系
 * 一个用户 -> 一个状态
 * 一个状态 -> 多个用户
 */
UserStatus.hasMany(User, {
  foreignKey: 'statusid',
  sourceKey: 'id'
})
User.belongsTo(UserStatus, {
  foreignKey: 'statusid',
  targetKey: 'id'
})

/**
 * 用户User 和好友 Friends 对应关系
 * 一个用户 -> 多个好友
 * 一个好友 -> 一个用户
 */
User.hasMany(Friends, {
  foreignKey: 'fid',
  sourceKey: 'id'
})
Friends.belongsTo(User, {
  foreignKey: 'fid',
  targetKey: 'id'
})

/**
 * 好友 Friends 和分组 FriendsGroup 对应关系
 * 一个好友 -> 一个分组
 * 一个分组 -> 多个好友
 */
FriendsGroup.hasMany(Friends, {
  foreignKey: 'fgid',
  sourceKey: 'id'
})
Friends.belongsTo(FriendsGroup, {
  foreignKey: 'fgid',
  targetKey: 'id'
})

/**
 * 用户 User 和分组 FriendsGroup 对应关系
 * 一个用户 -> 多个分组
 * 一个分组 -> 一个用户
 */
User.hasMany(FriendsGroup, {
  foreignKey: 'uid',
  sourceKey: 'id'
})
FriendsGroup.belongsTo(User, {
  foreignKey: 'uid',
  targetKey: 'id'
})


/**
 * 用户 User 和签名 Sign 对应关系
 * 一个用户 -> 一个签名
 * 一个签名 -> 一个用户
 */
Sign.hasOne(User, {
  foreignKey: 'signid',
  sourceKey: 'id'
})
User.belongsTo(Sign, {
  foreignKey: 'signid',
  targetKey: 'id'
})


module.exports = {
  User,
  Role,
  Permission,
  Code,
  Friends,
  FriendsGroup,
  Sign,
  UserStatus
}